﻿
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Growatt.DuiHelpers;
using Growatt.ShinebusServiceUploadPack;
using Growatt.ShinebusServiceUploadPack.Controllers;
using Microsoft.Extensions.Logging;


namespace Growatt.ShinebusServiceUploadPack.Service.OSS
{
   

    public class AuditFilesOnCloud
    {
        // 审核文件所需的参数
        public string softType = "yourSoftType";
        public string updateType = "1";
        public string fileName = "yourFileName";
        public string status = "2"; // 2通过, 3驳回
        public string source = "pc"; // pc/app
        public string verifyJobId = "yourVerifyJobId";
        public string verifyName = "yourVerifyName";
        public string rejection = ""; // 驳回原因，如果通过则为空
        public string version = "yourVersion";
        public string id = "yourId";

        public AuditFilesOnCloud(string softType, string updateType, string fileName, string status, string source, string verifyJobId, string verifyName, string rejection, string version, string id)
        {
            this.softType = softType;
            this.updateType = updateType;
            this.fileName = fileName;
            this.status = status;
            this.source = source;
            this.verifyJobId = verifyJobId;
            this.verifyName = verifyName;
            this.rejection = rejection;
            this.version = version;
            this.id = id;
        }
        private readonly IHttpClientFactory _httpClientFactory;
        public AuditFilesOnCloud(IHttpClientFactory httpClientFactory)
        {
            _httpClientFactory = httpClientFactory;
        }

        public async Task<AuditFilesOnCloudRootobject> AuditFilesOnCloudAsync(ILogger<UpdatePackController> logger, CancellationToken cancellationToken, string Server = "20.6.1.140:8081")
        {
            // 参数验证
            if (string.IsNullOrEmpty(softType) || string.IsNullOrEmpty(fileName) || string.IsNullOrEmpty(status) ||
                string.IsNullOrEmpty(source) || string.IsNullOrEmpty(verifyJobId) || string.IsNullOrEmpty(verifyName) ||
                string.IsNullOrEmpty(version) || string.IsNullOrEmpty(id))
            {
                logger.LogError("参数不完整，无法发送审核请求。");
                return null;
            }

            // 构建请求内容
            var content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("softType", softType),
                new KeyValuePair<string, string>("updateType", updateType),
                new KeyValuePair<string, string>("fileName", fileName),
                new KeyValuePair<string, string>("status", status),
                new KeyValuePair<string, string>("source", source),
                new KeyValuePair<string, string>("verifyJobId", verifyJobId),
                new KeyValuePair<string, string>("verifyName", verifyName),
                new KeyValuePair<string, string>("rejection", rejection),
                new KeyValuePair<string, string>("version", version),
                new KeyValuePair<string, string>("id", id),
            });

            string url = $"http://{Server}/pubData.do?op=auditUploadHttpFile";

            logger.LogInformation($"审核文件 发送POST请求到URL: {url}");

            // 创建HttpClient实例并设置超时时间
            using (HttpClient client = _httpClientFactory.CreateClient("OSS"))
            {

                try
                {
                    // 发送POST请求
                    HttpResponseMessage response = await client.PostAsync(url, content, cancellationToken);

                    // 检查响应状态码
                    if (response.IsSuccessStatusCode)
                    {
                        // 读取响应内容
                        string responseBody = await response.Content.ReadAsStringAsync(cancellationToken);
                        AuditFilesOnCloudRootobject apiResponse = DuiHelperJson.JsonToObject<AuditFilesOnCloudRootobject>(responseBody);
                        logger.LogInformation("审核文件回复，响应内容: " + responseBody);
                        return apiResponse;
                    }
                    else
                    {
                        logger.LogError($"审核文件失败，状态码: {response.StatusCode}");
                    }
                }
                catch (TaskCanceledException e)
                {
                    logger.LogError($"请求超时，错误信息: {e.Message}");
                }
                catch (Exception e)
                {
                    logger.LogError($"请求发生错误，错误信息: {e.Message}");
                }
            }

            return null;
        }
    }

    public class AuditFilesOnCloudRootobject
    {
        public int result { get; set; }
        public string msg { get; set; }
        public string obj { get; set; }
    }
}
